package com.cskaoyan.market.controller;

import com.cskaoyan.market.service.AdminOrderReplyService;
import com.cskaoyan.market.service.AdminOrderReplyServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/order/reply")
public class AdminOrderReplyController extends HttpServlet {
    AdminOrderReplyService adminOrderReplyService=new AdminOrderReplyServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        orderReply(req,resp);
    }

    private void orderReply(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String s = req.getReader().readLine();
        String commentId = JacksonUtil.parseString(s, "commentId");
        String content = JacksonUtil.parseString(s, "content");
        if (adminOrderReplyService.reply(commentId,content)){
            Object ok = ResponseUtil.ok();
            String str = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().println(str);
        }else {
            Object fail = ResponseUtil.fail();
            String f = JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().println(f);
        }
    }
}
